<template>
  <section>
    <el-card class="search-card">
      <BasicTableForm ref="formData"
        isScalingBtn
        :colNumOfRow="4"
        :formColumns="formItemList"
        :dataSource="formData"
        :defaultExpandStatus="false"
        :handleSearchForm="handleQueryForm" />
    </el-card>
    <el-card class="table-card">
      <basic-table
        :showSelectCol="false"
        :loading="tableInfo.loading"
        :columns="tableInfo.columns"
        :dataSource="tableInfo.list"
        :pagination="pagination"
        :tableHeight="460"
        @pageChange="handlePageChange"
        @pageSizeChange="handlePageSizeChange"
      >
        <div slot="content_context">
          <el-button
            v-if="showBtn('导出') && supplierExportConfig.supplier"
            :disabled="!pagination.total"
            size="mini"
            type="primary"
            icon="el-icon-download"
            :loading="exporting"
            @click="handleExportFile"
            >导出</el-button>
        </div>
      </basic-table>
    </el-card>
  </section>
</template>

<script>
import { mapGetters } from 'vuex';
import { getBtnsId } from '@/libs/btnsPermission';
import BasicTableForm from '@/components/supplier/BasicForm'; // 表单组件
import BasicTable from '@/components/supplier/BasicTable';
// import AdvancedTable from '@/components/supplier/AdvancedTable'; // 表格组件
// import BaseList from '@/mixins/supplier/baseList'; // 表格Mixin
import {
  querySupplierCarrierListService,
  exportSupplierCarrierFileService
} from '@/api/supplier/management/supplierCarrier';
import { createExcelByExport } from '@/utils/bizUtils';
import { getHasIndexTableData } from '@/pages/supplier/utils/bizUtils';
import { INITIAL_FIELDS_VALUE, FORM_ITEM_LIST, TABLE_COLUMNS } from './constants';

export default {
  // mixins: [ BaseList ], // 分页数据及方法
  components: {
    BasicTableForm,
    BasicTable,
    // AdvancedTable
  },
  computed: {
    ...mapGetters('permissions/systemBtns', ['showBtn']),
  },
  data () {
    const columns = TABLE_COLUMNS.map((i) => {
      if (i.key === 'supplierName') {
        i.formatter = (row, column, cellValue, index) => {
          return <router-link to={{ name: 'viewSupplierCarrierList', query: { id: row.supplierId } }}>{ row.supplierName }</router-link>
        }
      }
      return i
    })
    return {
      formItemList: FORM_ITEM_LIST,
      tableInfo: {
        list: [],
        selection: [],
        columns: columns,
        loading: false
      },
      pagination: {
        current: 1,
        total: 0,
        size: 10
      },
      formData: Object.assign({}, INITIAL_FIELDS_VALUE),
      toolbarConfig: {
        slots: {
          // 自定义工具栏模板
          buttons: 'toolbar_buttons',
        }
      },
      exporting: false
    };
  },
  created () {
    sessionStorage.setItem('buttonId', getBtnsId('查询'));
    this.getTableData();
  },
  methods: {
    handleExportFile () {
      this.exporting = true
      exportSupplierCarrierFileService(this.getRequestParams()).then((res) => {
        createExcelByExport({ excelTitle: '服务商/承运商信息', res })
        this.exporting = false
      })
    },
    handleQueryForm () {
      this.getTableData(1);
    },
    // 页码切换
    handlePageChange (val) {
      this.getTableData(val);
    },
    // 每页条数切换
    handlePageSizeChange (val) {
      this.getTableData(1, val);
    },
    // 获取列表查询参数
    getRequestParams () {
      const {
        supplierCode,
        supplierName,
        mainProduct,
        supplierClassify,
        mainCategoryId,
        supplierType,
        createUser
      } = this.formData
      return {
        supplierCode,
        supplierName,
        mainProduct,
        supplierClassify,
        mainCategoryId,
        supplierType,
        createUser
      }
    },
    async getTableData (current = this.pagination.current, size = this.pagination.size) {
      this.tableInfo.loading = true
      const params = Object.assign({ current, size }, this.getRequestParams())
      const resp = await querySupplierCarrierListService(params)
      this.tableInfo.loading = false
      if (resp && resp.result) {
        const { records, total, pages } = resp.result
        this.pagination = Object.assign(this.pagination, { current, size, total, pages })
        this.tableInfo.list = getHasIndexTableData(records, this.pagination)
      } else {
        this.tableInfo.list = []
      }
    },
  }
}
</script>
